package com.ruoyi.follow.service.impl;

import com.ruoyi.follow.domain.TbFollow;
import com.ruoyi.follow.mapper.TbFollowMapper;
import com.ruoyi.follow.service.ITbFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 关注Service业务层处理
 *
 * @author 劳雅玲
 * @date 2024-03-01
 */
@Service
public class TbFollowServiceImpl implements ITbFollowService {
    @Autowired
    private TbFollowMapper tbFollowMapper;

    /**
     * 查询关注
     *
     * @param id 关注主键
     * @return 关注
     */
    @Override
    public TbFollow selectTbFollowById(String id) {
        return tbFollowMapper.selectTbFollowById(id);
    }

    /**
     * 查询关注列表
     *
     * @param tbFollow 关注
     * @return 关注
     */
    @Override
    public List<TbFollow> selectTbFollowList(TbFollow tbFollow) {
        return tbFollowMapper.selectTbFollowList(tbFollow);
    }

    /**
     * 新增关注
     *
     * @param tbFollow 关注
     * @return 结果
     */
    @Override
    public int insertTbFollow(TbFollow tbFollow) {
        return tbFollowMapper.insertTbFollow(tbFollow);
    }

    /**
     * 修改关注
     *
     * @param tbFollow 关注
     * @return 结果
     */
    @Override
    public int updateTbFollow(TbFollow tbFollow) {
        return tbFollowMapper.updateTbFollow(tbFollow);
    }

    /**
     * 批量删除关注
     *
     * @param ids 需要删除的关注主键
     * @return 结果
     */
    @Override
    public int deleteTbFollowByIds(String[] ids, Long myUserid) {
        return tbFollowMapper.deleteTbFollowByIds(ids, myUserid);
    }

    /**
     * 删除关注信息
     *
     * @param id 关注主键
     * @return 结果
     */
    @Override
    public int deleteTbFollowById(String id) {
        return tbFollowMapper.deleteTbFollowById(id);
    }

    @Override
    public Boolean addOrCancel(TbFollow tbFollow) {
        // 如果有数据就删除，如果没有数据就添加
        if (Objects.isNull(tbFollow.getMyUserId()) || Objects.isNull(tbFollow.getUserId())) {
            return false;
        }
        TbFollow tbFollows = tbFollowMapper.selectTbFollow(tbFollow);

        if (Objects.isNull(tbFollows)) {
            // 如果没有查到数据，走关注逻辑
            tbFollow.setId(null);
            insertTbFollow(tbFollow);
        }
        if (!Objects.isNull(tbFollows)) {
            // 取消关注
            tbFollowMapper.deltedTbFollow(tbFollow);
        }
        return true;
    }
}
